// pages/mysearch/mysearch.js
let {
  getsearch,
  gethot
} = require("../../api/mysearch")
const {
  getgoods
} = require("../../api/mysearch_json");
const {
  getmyhot
} = require("../../api/hot_json");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: "",
    keylists: [],
    mylists: null,
    one: true,
    two: false,
    three: false,
    hotlists: [],
    // hideit:false
  },

  find(e) {
    // let keyword = e.detail.value
    this.setData({
      keyword: e.detail.value //得到关键字
    })
  },
  // 点击历史记录
  lishi(item){
    console.log(item.currentTarget.dataset.val)
    let lishici = item.currentTarget.dataset.val
    this.setData({
      keyword:lishici
    })
  },
  okfind() { //提交关键字

    let {
      keyword,
      keylists,
      one,
      two,
      three,
      mylists,
    } = this.data;


    // 解构
    if (keyword != "") {
      keylists=[]
      keylists.push(keyword)
      wx.setStorageSync('keylists', keylists)
      this.setData({keylists})
    } else {
      one = true
      two = false
      three = false
      this.setData({one,two,three})
      return;
    }
    //一搜索页面就先让他等于本地的数据。后面请求到了数据再把页面上的数据改成请求道的数据。
    let data = getgoods
    let mysearch = data.data.list_v2 //搜索到的商品的数据
    mylists = mysearch
    one = false
    two = false
    three = true
    this.setData({mylists,one,two,three});


    // 请求数据
    getsearch({
      query: this.data.keyword //传递关键字
    }).then(data => {
      // 判断盒子显示
      if (!data) {
        one = false
        two = true
        three = false
        this.setData({one,two,three})
        return;
      } else {
        console.log(data) //数据
        // console.log(data.data.list_v2)
        one = false
        two = false
        three = true
      }

      //可以请求到接口，但是数组没东西
      if (!data.data.list_v2.length) {
        one = false
        two = true
        three = false
        this.setData({one,two,three})
        return;
      }

      let mysearch = data.data.list_v2 //搜索到的商品的数据
      mylists = mysearch
      this.setData({mylists})

    })
  },
  // 删除历史记录
  clear() {
    this.setData({
      keylists: [],
      kkklists: []
    })
    wx.setStorageSync('keylists', this.data.keylists)

    // if(this.data.keylists!=[]){
    //   this.setData({
    //     hideit:true
    //   })
    // }else{
    //   this.setData({
    //     hideit:false
    //   })
    // }
  },
  // 点击搜索发现
  souta(e) {
    let hotname = e.currentTarget.dataset.name
    this.setData({
      keyword: hotname
    })

    console.log(hotname)
  },

  /**
   * 生命周期函数--监听页面加载
   */


  onLoad(options) {
    this.data.hotlists = getmyhot
    console.log(this.data.hotlists)
    this.setData({
      hotlists: this.data.hotlists
    })
    gethot().then(data => {
      this.setData({
        hotlists: data.data.hot_class
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
        // kkklists: wx.getStorageSync('keylists'),
        keylists: wx.getStorageSync('keylists'),
      }),
      // console.log(this.data.kkklists)
      console.log(this.data.keylists)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})